<?php
//session_start();
require_once( 'master_db.php' );
 
$type = new M_master();
$user_id = $_SESSION['userid'];

if (isset($_POST['m'])  &&  $_POST['m']!=""){
    if ($_POST['m']== 'load'){
		
	  $name = $_POST['name'];
	  $stype = $_POST['type'];
	  
      $result=$type->loadProduct($name,$stype);
      $rows=$type->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
            while($row = $result->FetchRow()){
               $data[] = array(0,$row['id'],$row['code'],$row['name'],$row['cost'], $row['price'],$row['type_name'], $row['status']) ;
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
      
    }else if ($_POST['m']=='add'){
	     $ar = array();
		$ar['name']=  $_POST['name'];
		$ar['code']=  $_POST['code'];
		$ar['type'] = $_POST['type'];
		$ar['cost'] = $_POST['cost'];
		$ar['price'] = $_POST['price'];
		$ar['status'] = $_POST['s'];
	 
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
		
		$return_id  = $type->checkProductCode($_POST['code']);
		if ($return_id==0){
			$id=$type->insertDataWithSql("SELECT code,name,type,cost,price,status,createdate,createby FROM tb_product where 1=2 ",$ar);
			echo 1;
		}else {
			echo 0;
		}
		
    }else if ($_POST['m']=='edit'){
		$ar = array();
		$ar['name']=  $_POST['name'];
		$ar['code']=  $_POST['code'];
		$ar['type'] = $_POST['type'];
		$ar['cost'] = $_POST['cost'];
		$ar['price'] = $_POST['price'];
		$ar['status'] = $_POST['s'];
		$ar['updatedate'] =date( "Y/m/d H:i:s" );
		$ar['updateby'] =$user_id;
		
		$strcipt = $type->updateDataWithSql("SELECT code,name,type,cost,price,status,createdate,createby FROM tb_product where id=".$_POST['id'],$ar);
		// echo json_encode($strcipt);
	}
	 
	else if ($_POST['m']=='load_type'){
	  $result=$type->load_type();
      $rows=$type->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
            while($row = $result->FetchRow()){
               $data[] = array(0,$row['id'],$row['name'],$row['status']) ;
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
	}else if ($_POST['m']=='add_type'){
        $ar = array();
		$ar['name']=  $_POST['name'];
		//$ar['detail'] = $_POST['detail'];
		$ar['status'] = $_POST['s'];
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
	 
		$id=$type->insertDataWithSql(" SELECT name, status , createdate, createby from tb_product_type where 1=2 " ,$ar);
		
    }else if ($_POST['m']=='edit_type'){
		$ar = array();
		$ar['name']=  $_POST['name'];
		//$ar['detail'] = $_POST['detail'];
		$ar['status'] = $_POST['s'];
		$ar['updatedate'] =date( "Y/m/d H:i:s" );
		$ar['updateby'] =$user_id;
		$strcipt = $type->updateDataWithSql(" SELECT name , status , updatedate, updateby from tb_product_type where id =". $_POST['id'],$ar);
		// echo json_encode($strcipt);
	}
	else if ($_POST['m']=='del'){
         $id=$_POST['id'];
		 $type->setTable($_POST['tb']);  
		 $type->deleData($id);
		 
    }
}else{
 	if (isset($_GET['m']) && $_GET['m'] !="" ){
			echo json_encode( $type->getType());
	}
   
}
 
?>